from data_output import write_to_py_file
from freq_gen import freq_lis
import numpy as np

#频谱数据文件名
freq_file_name = ''
#乐谱数据文件名
time_file_name = ''

# 读取文件内容
with open(freq_file_name, "r", encoding="utf-8") as freq_filedata:
    freq_fileinfo = freq_filedata.read()

with open(time_file_name, "r", encoding="utf-8") as time_filedata:
    time_fileinfo = time_filedata.read()

# 创建一个空字典作为全局命名空间
global_namespace_1 = {}
exec(freq_fileinfo, global_namespace_1)

global_namespace_2 = {}
exec(time_fileinfo, global_namespace_2)

# 假设数组变量名为 data
freq_res = global_namespace_1.get("res_list")
time_res = global_namespace_2.get("res_list")

print('instrument_type?')
instrument_type = input('>')

instrument_type_code = list(np.zeros(8))

if instrument_type == '提琴' or instrument_type == '1':
    instrument_type_code[1] = 1
elif instrument_type == '钢琴' or instrument_type == '2':
    instrument_type_code[2] = 1
elif instrument_type == '吉他' or instrument_type == '3':
    instrument_type_code[3] = 1
elif instrument_type == '笛子' or instrument_type == '4':
    instrument_type_code[4] = 1
elif instrument_type == '铜管' or instrument_type == '5':
    instrument_type_code[5] = 1
elif instrument_type == 'misc' or instrument_type == '0':
    instrument_type_code[0] = 1
else:
    instrument_type_code[0] = 1

data_set = []

for item in freq_res:
    for time_win in time_res:
        time_cross = time_win[1] - time_win[0]
        if item[-1]*1000 > time_win [0] + time_cross * 0.05 and item[-1]*1000 < time_win[1] - time_cross * 0.05:
            data_set.append([item[:-1], item[-1], time_win[2], instrument_type_code])

for item in data_set:
    dect_freq_lis = item[2].split('+')
    print(dect_freq_lis)
    freq_data = np.zeros(88)
    for freq in dect_freq_lis:
        for n in range(len(freq_lis)):
            if freq == freq_lis[n][2]:
                freq_data[n] = 1
                print(n)
    item[2] = list(freq_data)



print(freq_res)
print(time_res)
print(data_set)

print('-'*20)
for i in range(10):
    print(data_set[0])

write_to_py_file(output_py_name='try_data_set.py', output_data=data_set, write_mode='str')